#include "system.h"
#include "lpc213x.h"


#define PLOCK 0x400
#define SLOW_IO

void feed(void) {
	PLLFEED = 0xAA;
	PLLFEED = 0x55;
}

void system_Init(void) {

	// Setting Multiplier and Divider values
	// PLLCFG (0-4) = M-1 = 5 = 0b00101
	// PLLCFG (5-6) = P = Fcco / (2xCclk) = 2

	PLLCFG = 0x25;
	feed();

	// Enabling the PLL */
	PLLCON = 0x1;
	feed();

	// Wait for the PLL to lock to set frequency
	while (!(PLLSTAT & PLOCK))
		;

	// Connect the PLL as the clock source
	PLLCON = 0x3;
	feed();

	// Enabling MAM and setting number of clocks used for Flash memory fetch (4 cclks in this case)
	MAMCR = 0x0;
	MAMTIM = 0x3;
	MAMCR = 0x2;

	// Setting peripheral Clock (pclk) to System Clock (cclk)
	VPBDIV = 0x1;

#ifdef SLOW_IO
#elif FAST_IO
	// ENABLE FIO //on both ports!
	SCS |= 3;
#endif
}

